<!DOCTYPE html>
<html>

	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" href="css/bootstrap.min.css" />
	</head>

	<body>
		<nav class="navbar navbar-default navbar-fixed-top">
			<div class="container">
				<div class="row">
					<div class="navbar-header">
						<a href="index.html" class="navbar-brand">
							<img src="img/logo1.png" style="height: 50px;margin-top: -15px;" />
						</a>
					</div>
					<div>
						<p class="navbar-text" style="font-size: x-large;margin: 8px 15px;">ElasticSearch</p>
					</div>

					<div class="navbar-form navbar-right" style="margin-right: 0px;">
						<div class="input-group">
							<input id="searchKey" type="text" class="form-control" placeholder="搜索关键字" onkeypress="if(event.keyCode == 13) EsSearch();">
							<span class="input-group-btn">
								<button class="btn btn-primary" onclick="EsSearch();"><span class="glyphicon glyphicon-search"></span></button>
							</span>
						</div>
					</div>
					<!--<a onclick="advanceSearch();" href="javascript:;" class="navbar-right" onclick="" style="margin: 15px 15px; color: #777;">
						高级搜索				
					</a>-->
				</div>
			</div>
		</nav>
		<div style="height: 60px;"></div>

		<div class="container">
			<!-- 高级搜索 -->
			<div class="row">
				<div class="col-xs-10 col-xs-offset-1">
					<div class="list-group">
						<div class="text-center"><h4>加载中...</h4></div>
						<!-- <div class="list-group-item" style="border: 0;">
							<a href="#" style="color: #0F0F0F;"><h4>1.神秘客</h4></a>
							<p class="text-muted">
								<small>发布时间:2015-2-11</small>
								<small class="pull-right">
									点击量：<span class="badge">20</span>
								</small>
							</p>
							<p class="text-muted">岁的女孩微风你哦额的看法为哦符合符合被覅我发货胃口恶风还未哦</p>
							<p>
								<span class="badge">关键字</span>
								<span class="badge">关键字</span>
								<span class="badge">关键字</span>
							</p>
						</div>
						<div style="border: 1px dashed #ddd;"></div>
						
						<div class="list-group-item" style="border: 0;">
							<a href="#" style="color: #0F0F0F;"><h4>2.神秘客</h4></a>
							<p class="text-muted">
								<small>发布时间:2015-2-11</small>
								<small class="pull-right">
								点击量：<span class="badge">20</span>
							</small>
							</p>
							<p class="text-muted">岁的女孩微风你哦额的看法为哦符合符合被覅我发货胃口恶风还未哦</p>
							<p>
								<span class="badge">关键字</span>
								<span class="badge">关键字</span>
								<span class="badge">关键字</span>
							</p>
						</div>
						<div style="border: 1px dashed #ddd;"></div> -->
					</div>
					<!-- 分页 -->
					<div class="text-center">
						<ul class="pagination">
							<li><a href="javascript:;"><span class="glyphicon glyphicon-chevron-left"></span></a></li>
							<li><a href="javascript:;">1</a></li>
							<li><a href="javascript:;">2</a></li>
							<li><a href="javascript:;">3</a></li>
							<li><a href="javascript:;">4</a></li>
							<li><a href="javascript:;"><span class="glyphicon glyphicon-chevron-right"></span></a></li>
						</ul>
						<div>
							共 <span id="realPageNum"></span> 页
						</div>
						<div class="input-group col-xs-2 col-xs-offset-5" style="margin-bottom: 50px;">
			                <input id="goNum" type="text" class="form-control" onkeypress="if(event.keyCode == 13) goPageNum();">
			                <span class="input-group-btn">
			                    <button class="btn btn-primary" type="button" onclick="goPageNum();">
			                       Go
			                    </button>
			                </span>
			            </div>
					</div>
				</div>
			</div>
		</div>
		<!--<script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>-->
		<script src="//cdn.bootcss.com/jquery/1.12.0/jquery.min.js"></script>
		<script type="text/javascript" src="js/bootstrap.min.js"></script>
	    <!--高级搜索的显隐-->
		<script type="text/javascript">
			function advanceSearch(){
				$("#advanceSearch").toggle();
			}
		</script>
		<!--分页js-->
		<script type="text/javascript">
			var maxPageNum = 5;
			var RealPageNum = 8;
			$(function() {
				addPage();
			});
			
			function addPage() {
				if (RealPageNum >= maxPageNum) {
					var pageHtml = "<li><a href='javascript:;' onclick='PagePrevPage(" + 1 + ")'><span class='glyphicon glyphicon-chevron-left'></span></a></li>";
					for (var i = 1; i < maxPageNum + 1; i++) {
						if (i == 1) {
							pageHtml += "<li class='active'><a href='javascript:;' onclick='PageLiClick(" + i + ")'>" + i + "</a></li>"
						} else {
							pageHtml += "<li><a href='javascript:;' onclick='PageLiClick(" + i + ")'>" + i + "</a></li>"
						}
					}
					pageHtml += "<li><a href='javascript:;' onclick='NextPage(" + 1 + ")'><span class='glyphicon glyphicon-chevron-right'></span></a></li>";
					$(".pagination").html(pageHtml);
				} else {
					var pageHtml = "<li><a href='javascript:;' onclick='PagePrevPage(" + 1 + ")'><span class='glyphicon glyphicon-chevron-left'></span></a></li>";
					for (var i = 1; i < RealPageNum + 1; i++) {
						if (i == 1) {
							pageHtml += "<li class='active'><a href='javascript:;' onclick='PageLiClick(" + i + ")'>" + i + "</a></li>"
						} else {
							pageHtml += "<li><a href='javascript:;' onclick='PageLiClick(" + i + ")'>" + i + "</a></li>"
						}
					}
					pageHtml += "<li><a href='javascript:;' onclick='NextPage(" + 1 + ")'><span class='glyphicon glyphicon-chevron-right'></span></a></li>";
					$(".pagination").html(pageHtml);
				}
			}

			function PagePrevPage(pageNum) {
				if (pageNum > 1) {
					PageLiClick(pageNum - 1);
				}
			}

			function NextPage(pageNum) {
				if (pageNum < RealPageNum) {
					PageLiClick(pageNum + 1);
				}
			}
			
			var nowNum = 0;
			function PageLiClick(pageNum) {
				nowNum = pageNum;
				/*var pageHtml = "<li><a href='javascript:;' onclick='PagePrevPage(" + pageNum + ")'><span class='glyphicon glyphicon-chevron-left'></span></a></li>";
				var minNum = 0;
				var maxNum = 0;
				if (RealPageNum >= maxPageNum) {
					if (pageNum - 2 < 1) {
						minNum = 1;
						maxNum = minNum + 5;
					} else {
						minNum = pageNum - 2;
						if (pageNum + 2 > RealPageNum) {
							maxNum = RealPageNum + 1;
							minNum = maxNum - 5;
						} else {
							maxNum = pageNum + 2 + 1;
						}
					}
				} else {
					minNum = 1;
					maxNum = RealPageNum + 1;
				}
				for (var i = minNum; i < maxNum; i++) {
					if (pageNum == i) {
						pageHtml += "<li class='active'><a href='javascript:;' onclick='PageLiClick(" + i + ")'>" + i + "</a></li>"
					} else {
						pageHtml += "<li><a href='javascript:;' onclick='PageLiClick(" + i + ")'>" + i + "</a></li>"
					}
				}
				pageHtml += "<li><a href='javascript:;' onclick='NextPage(" + pageNum + ")'><span class='glyphicon glyphicon-chevron-right'></span></a></li>";
				$(".pagination").html(pageHtml);*/
				var searchText = $("#searchKey").val();
				getData(searchText,5,pageNum-1);
			}
			
			function fenye(pageNum){
				var pageHtml = "<li><a href='javascript:;' onclick='PagePrevPage(" + pageNum + ")'><span class='glyphicon glyphicon-chevron-left'></span></a></li>";
				var minNum = 0;
				var maxNum = 0;
				if (RealPageNum >= maxPageNum) {
					if (pageNum - 2 < 1) {
						minNum = 1;
						maxNum = minNum + 5;
					} else {
						minNum = pageNum - 2;
						if (pageNum + 2 > RealPageNum) {
							maxNum = RealPageNum + 1;
							minNum = maxNum - 5;
						} else {
							maxNum = pageNum + 2 + 1;
						}
					}
				} else {
					minNum = 1;
					maxNum = RealPageNum + 1;
				}
				for (var i = minNum; i < maxNum; i++) {
					if (pageNum == i) {
						pageHtml += "<li class='active'><a href='javascript:;' onclick='PageLiClick(" + i + ")'>" + i + "</a></li>"
					} else {
						pageHtml += "<li><a href='javascript:;' onclick='PageLiClick(" + i + ")'>" + i + "</a></li>"
					}
				}
				pageHtml += "<li><a href='javascript:;' onclick='NextPage(" + pageNum + ")'><span class='glyphicon glyphicon-chevron-right'></span></a></li>";
				$(".pagination").html(pageHtml);
			}
		</script>
		<script type="text/javascript">
			/*主页跳转过来时的搜索js*/
			$(function(){
				var params = location.search.substring(1);
				var searchText = params.split("=")[1];
				searchText = decodeURI(searchText);
				$("#searchKey").val(searchText);
				//alert(searchText);
				nowNum = 1;
				getData(searchText,5,0);
				//PageLiClick(1);
				
			});
			/*搜索框js*/
			function EsSearch(){
				var searchText = $("#searchKey").val();
				nowNum = 1;
				getData(searchText,5,0);	
				//PageLiClick(1);
			}
			/* 页面跳转js */
			function goPageNum(){
				var goNum = $("#goNum").val();
				goNum = parseInt(goNum);
				PageLiClick(goNum);
			}
			
			/*ajax从后台获取数据*/
			function getData(searchText,size,pageNum){
				$(".list-group").html("<div class='text-center'><h4>加载中...</h4></div>");
				$.ajax({
						url : "/nutch_es-web/getData",
						type : "POST",
						data: {"searchText":searchText,"size":size,"pageNum":pageNum},
						async : true,
						success : function(data){
							//alert(data);
							//console.log(data);
							//取数据
							var result = JSON.parse(data);
							var item = result.hits.hits;
							RealPageNum = Math.ceil(result.hits.total/size);
							$("#realPageNum").html(RealPageNum);
							fenye(nowNum);
							//addPage();
							//alert(RealPageNum);
							var titles = [];
							var urls = [];
							var contents = [];
							var keys = [];
							var outlines = [];
							var times = [];
							for(var i=0; i<item.length; i++){
								titles[i] = result.hits.hits[i]._source.title;
								console.log(titles[i]);
							}
							for(var i=0; i<item.length; i++){
								urls[i] = result.hits.hits[i]._source.url;
								console.log(urls[i]);
							}
							for(var i=0; i<item.length; i++){
								times[i] = result.hits.hits[i]._source.tstamp.substring(0,10);
								console.log(times[i]);
							}
							for(var i=0; i<item.length; i++){
								contents[i] = result.hits.hits[i]._source.content;
								//console.log(contents[i]);
								$("#unuse").html(contents[i]);
								//alert($("#unuse .zm-item-tag").length);
								outlines[i] = $("#unuse #zh-question-detail .zm-editable-content").html();
								if(outlines[i] == undefined){
									outlines[i] = "";
								}
								//alert(outlines[i]);
								keys[i] = [];
								for(var j=0; j<$("#unuse .zm-item-tag").length; j++){
									keys[i][j] = $("#unuse .zm-item-tag").get(j).innerHTML;
									//alert(keys[i][j]);
								}
								//alert($("#unuse .zm-item-tag").html());
							}
							//console.log(item.length);
							//console.log(result.hits.hits[0]._source.title);
							//console.log(result.hits.hits[0]._source.url);
							//console.log(result.hits.hits[0]._source.content);
							//alert(result.hits.hits[0]._source.content);
							//拼显示的html代码
							var listHtml = "";
							for(var i=0; i<item.length; i++){
								listHtml += "<div class='list-group-item' style='border: 0;'>"+
												"<a href='"+ urls[i] +"' style='color: #0F0F0F;'><h4>"+ (i+1) + "." + titles[i] +"</h4></a>"+
												"<p class='text-muted'>"+
													"<small>获取时间:"+ times[i] +"</small>"+
												"</p>"+ 
												"<p class='text-muted'>"+ outlines[i] +"</p>"+
												"<p>";
								for(var j=0; j<keys[i].length; j++){
									listHtml += "<span class='badge'>"+ keys[i][j] +"</span>";
								}
								listHtml += "</p>"+
											"</div>"+
											"<div style='border: 1px dashed #ddd;'></div>";
							}
							if(listHtml == ""){
								listHtml = "<div class='text-center'><h4>未搜索到任何结果</h4></div>";
							}
							$(".list-group").html(listHtml);
							$("#unuse").html("");
						},
						error : function(e){
							alert("加载失败，请重试！");
						}
				});
			}
		</script>
		<div id="unuse" hidden="hidden"></div>
	</body>

</html>